package telnetserver;

import java.util.Random;

public class SnakeGame extends Thread {
    private Telnet telnet;
    private Object ret;
    private boolean run;
    private User user;
    private int[][] snake;
    private int way;
    private int[] apple;
    
    public SnakeGame(Telnet t, Object r, User u) {
        u.setButtons(null, null);
        telnet = t;
        ret = r;
        user = u;
        run = true;
        snake = new int[1][2];
        snake[0][0] = 2;
        snake[0][1] = 10;
        way = 3;
        apple = new int[2];
    }
    
    public void run() {
        try {
            boolean crash = false;
            user.clear();
            generateApple();
            user.prepare(apple[0], apple[1], "X");
            String s = "";
            for (int i = 0; i < 78; i++) {
                s += "-";
            }
            user.prepare(1, 1, "+" + s + "+");
            for (int i = 2; i < 23; i++) {
                user.prepare(1, i, "|");
                user.prepare(80, i, "|");
            }
            user.prepare(1, 23, "+" + s+ "+");
            
            user.prepare(1, 24, "(Press enter/return to go back).   SCORE: " + user.getScore());
            
            while (run) {
                int last = snake.length - 1;
                int newx = snake[last][0];
                int newy = snake[last][1];
                if (way == 1) newy = snake[last][1] - 1;
                else if (way == 2) newy = snake[last][1] + 1;
                else if (way == 3) newx = snake[last][0] + 1;
                else if (way == 4) newx = snake[last][0] -1;
                user.prepare((char)27 + "[s");
                user.prepare(snake[0][0], snake[0][1], " ");
                for (int i = 0; i < snake.length - 1; i++) {
                    snake[i][0] = snake[i + 1][0];
                    snake[i][1] = snake[i + 1][1];
                    if (snake[i][0] == newx && snake[i][1] == newy) crash = true;
                    user.prepare(snake[i][0], snake[i][1], "0");
                    user.prepare((char)27 + "[u");
                }
                snake[last][0] = newx;
                snake[last][1] = newy;
                user.prepare(newx, newy, "@");
                user.prepare((char)27 + "[u");
                
                if (newx == 1 || newx == 80 || newy == 1 || newy == 23) crash = true;
                if (newx == apple[0] && newy == apple[1]) {
                    generateApple();
                    user.prepare(apple[0], apple[1], "X");
                    add();
                }
                
                
                if (crash) {
                    user.prepare(10, 10, "+-----------------------+");
                    user.prepare(10, 11, "|                       |");
                    user.prepare(10, 12, "|      You crashed!     |");
                    user.prepare(10, 13, "|     -Press return-    |");
                    user.prepare(10, 14, "|                       |");
                    user.prepare(10, 15, "+-----------------------+");
                    run = false;
                    user.setScore(0);
                }
                
                user.send();
                Thread.sleep(100);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    public void add() {
        int[][] temp = new int[snake.length + 1][2];
        for (int i = 0; i < snake.length; i++) {
            temp[i + 1][0] = snake[i][0];
            temp[i + 1][1] = snake[i][1];
        }
        int x = snake[0][0];
        int y = snake[0][1];
        if (way == 1) y--;
        else if (way == 2) y++;
        else if (way == 3) x--;
        else if (way == 4) x++;
        temp[0][0] = x;
        temp[0][1] = y;
        snake = new int[temp.length][2];
        for (int i = 0; i < temp.length; i++) {
            snake[i][0] = temp[i][0];
            snake[i][1] = temp[i][1];
        }
        user.setScore(user.getScore() + 1);
        user.prepare(43, 24, "" + user.getScore());
    }
    
    public void read(User u, String s) {
        run = false;
        telnet.changeMode(u, ret);
    }
    
    public void buttonPressed(User u, int i) {
        if (i == 1 && way != 2) way = i;
        else if (i == 2 && way != 1) way = i;
        else if (i == 3 && way != 4) way = i;
        else if (i == 4 && way != 3) way = i;
    }
    
    public void remUser(User u) {
        run = false;
    }
    
    public void generateApple() {
        Random r = new Random();
        apple[0] = 3 + r.nextInt(70);
        apple[1] = 2 + r.nextInt(20);
    }
}
